package handles

import (
	"github.com/gin-gonic/gin"
	"order-pro/models"
	"strconv"
)

func RoleNoelAdd(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))

	data := models.RoleNode{

		Rid: rid,
		Nid: nid,
	}

	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "创建失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "创建成功",
	})
	return

}

func RoleNoelList(c *gin.Context) {

	nid := c.Query("nid")
	/*
		SELECT
		 *
		FROM
		  nodes
		  LEFT JOIN role_nodes ON nodes.id = role_nodes.nid
		  WHERE nodes.id=1
	*/

	var roleNoel []models.Node

	models.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON nodes.id = role_nodes.nid").
		Where("role_nodes.id=?", nid).Find(&roleNoel)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": roleNoel,
	})
	return
}

func UserRoleList(c *gin.Context) {
	uid := c.Query("uid")
	/*
		SELECT
		  *
		FROM
		  roles
		  LEFT JOIN user_roles ON roles.id = user_roles.uid
	*/

	var userRole []models.Role
	models.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON roles.id = user_roles.uid").
		Where("user_roles.id=?", uid).Find(&userRole)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
		"data": userRole,
	})
	return
}
